所以我有这些带有输入复选框的标签。现在提交后,表单不接受,它返回 here ,我希望它选择上一个选中的框。
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Zo maak je van onbekenden klanten1">Zo maak je van onbekenden klanten<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Multi media feest">Multi media feest<span class="left" ></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Pretwerk OPTIE!">Pretwerk OPTIE!<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="use your apple4">use your apple<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Snelheid is geld4">Snelheid is geld<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Fotograferen met je iPhone">Fotograferen met je iPhone<span class="left" "></span></label>
我已经使用了隐藏字段等。所以我得到了之前输入的返回值。
var first = "<?php echo $first ?>";
//in this case I make first the value 'use your apple1'
$("[name$=group1][value=" + first + "]").prop("checked", "true");
现在它应该检查 group1 中的使用你的苹果框 但我得到了错误
jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: [name$=group1][value=use your apple1]
答案 0 :(得分:2)
添加单引号(&#39;)value=" + first + "]" to value='" + first + "']"
var first = "<?php echo $first ?>";
//in this case I make first the value 'use your apple1'
$("[name$=group1][value='" + first + "']").prop("checked", "true");
$(document).ready(function(){
var first = "use your apple1";
//in this case I make first the value 'use your apple1'
$("[name$=group1][value='" + first + "']").prop("checked", "true");
//console.log(str);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Zo maak je van onbekenden klanten1">Zo maak je van onbekenden klanten<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Multi media feest">Multi media feest<span class="left" ></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Pretwerk OPTIE!">Pretwerk OPTIE!<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="use your apple4">use your apple<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Snelheid is geld4">Snelheid is geld<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Fotograferen met je iPhone">Fotograferen met je iPhone<span class="left" "></span></label>
&#13;
答案 1 :(得分:-1)
更改此
$("[name$=group1][value=" + first + "]").prop("checked", "true");
到这个
$("input[name=group1][value=" + first + "]").prop("checked", "true");