获取单选按钮值,但存在可变问题
Grabbing radio button values but having variable issues
编辑、更新
非常感谢Shailendra Sharma的帮助:)不过,每个人的回答都教会了我一些东西,所以谢谢大家!
首先,这是我的问题:
我正在努力使其在单击单选按钮时,它将在两个表单字段中将单选按钮的值添加到两个不同的变量中(选择1和2)
之后,它将把这些路径添加到$htmlImagePath变量中。在用户点击两个选项后,它将完成该路径,并且它将是一个图像源路径,此时我将有代码将该完整路径附加到HTML并显示图像。
不幸的是,我遇到了一个问题。我不太确定我做错了什么,但原始的console.log显示了两个值应该在的"对象"(在本例中为"cs"answers"schwarz"),当我单击表单按钮时,什么都没有发生。
我尝试了一些不同的方法,但遇到了类似的问题。完全卡住了,我的大脑现在被炸了帮助XD-
这是JS fiddle:https://jsfiddle.net/1qk59sk7/
$(document).ready(function() {
var $htmlImagePath = "<img src='images/";
var $selection1 = ($("input[type=radio][name='finishes'][value='cs']").prop("checked", true));
var $selection2 = ($("input[type=radio][name='colours'][value='schwarz']").prop("checked", true));
$("#finish input:radio").on("click", function() {
$selection1 = $("#finish input:checked").val();
});
$htmlImagePath = $htmlImagePath + $selection1 + '-';
$("#color input:radio").on ("click", function() {
$selection2 = $("#color input:checked").val();
});
$htmlImagePath = $htmlImagePath + $selection2 + ".jpg'";
console.log($htmlImagePath);
});
这是HTML
<div id="finish" class="spalte3">
<p >Ausfuehrungen Rahmen:</p>
<form>
<div>
<input type="radio" name="finishes" value="cs" class="image_selection">
<label for="cs">Chrome glanz</label>
</div>
<div>
<input type="radio" name="finishes" value="cbr" class="image_selection">
<label for="cbr">Chrome gebuerstet</label>
</div>
<div>
<input type="radio" name="finishes" value="cbl" class="image_selection">
<label for="cbl">Schwarz verchromt</label>
</div>
</div>
</form>
<div id="color" class="spalte3">
<p>Ausfuehrungen Tablare:</p>
<form>
<div>
<input type="radio" name="colours" value="schwarz" class="image_selection">
<label for="schwarz">Schwarz</label>
</div>
<div>
<input type="radio" name="colours" value="weiss-s" class="image_selection">
<label for="weiss-s">Weiss</label>
</div>
<div>
<input type="radio" name="colours" value="dunkelrot" class="image_selection">
<label for="dunkelrot">Dunkelrot</label>
</div>
<div>
<input type="radio" name="colours" value="fango" class="image_selection">
<label for="fango">Fango</label>
</div>
</form>
</div>
<div id="log" class="spalte3">
<img class="skizze-res" src="images/poolfinish/cs-schwarz.jpg" alt="Chrom glanz mit Schwarz">
</div>
当您的代码在页面上运行时,通过以下方式$htmlImagePath = $htmlImagePath + $selection1 + '-';
将对象分配给$htmlImagePath由于您的代码在页面加载后这一行永远不会执行第二次,这就是为什么$htmlImagePath
永远不会用新值更新
查看此代码
$(document).ready(function(){
var $htmlImagePath = "<img src='images/";
var $selection1 = ($("input[type=radio][name='finishes'][value='cs']").prop ("checked", true));
var $selection2 = ($("input[type=radio][name='colours'][value='schwarz']").prop ("checked", true));
$("#finish input:radio,#color input:radio").on ("click", function(){
$selection1 = $("#finish input:checked").val();
$selection2 = $("#color input:checked").val();
$htmlImagePath = $htmlImagePath + $selection1 + '-';
$htmlImagePath = $htmlImagePath + $selection2 + ".jpg'";
console.log($htmlImagePath);
});
});
这里是小提琴
之所以会发生这种情况,是因为变量$htmlImagePath
在运行时被赋值并存储[object..]
本身,您应该在单击处理程序的两个部分中添加值。尝试以下代码:
$("#finish input:radio").on ("click", function(){
$selection1 = $("#finish input:checked").val();
$htmlImagePath = $htmlImagePath + $selection1 + '-';
newPath();
});
$("#color input:radio").on ("click", function() {
$selection2 = $("#color input:checked").val();
$htmlImagePath = $htmlImagePath + $selection2 + ".jpg'";
newPath();
});
function newPath(){
console.log($htmlImagePath);
}
DEMO
$selection1
和$selection2
是JQuery对象,您应该使用它们持有的值来连接,如下所示:
$selection1.val()
$selection2.val()
这是更新JSFiddle,其中console.log显示<img src='images/cs-schwarz.jpg'
校正行:
$htmlImagePath = $htmlImagePath + $selection1.val() + '-';
$htmlImagePath = $htmlImagePath + $selection2.val() + ".jpg'";
- IE9的HTML5 Canvas getImageData()函数存在问题
- Onclick不会启动..可能存在javascript语法问题
- JSP 中的“日期”字段存在问题
- 为什么JavaScript中存在单态和多态性问题
- JavaScript对象中存在数组的问题
- 在 AngularJS 中显示信息的标记存在问题
- InnerHTML创建问题,没有't加载顺序正确,可能存在处理问题
- javascript中的数组和if语句存在问题
- TypeError:$(..).higharts不是函数-现有解决方案不起作用-Yeoman生成器存在问题
- 我的Firefox插件和跨域https存在问题
- jquery中的setOptions存在问题
- 数据在确认框中的显示方式存在问题
- AngularJS中存在与$scope变量相关的问题.UI中的值未更改
- Safari存在文本输入问题,用户输入时会选择文本,导致文本丢失
- javascript className函数存在问题
- 存在从函数返回值的问题
- document.referrer存在问题
- 日期选择器UI存在问题
- Javascript;promise中存在Q-闭包问题
- 可能存在Javascript范围问题