简体   繁体   English

选择复选框,单选按钮和列表

[英]Selecting Checkboxes, Radio buttons and list

I have a problem checking/selecting radiobuttons, checkboxes and list styles at runtime using jQuery/jQuery Mobile. 我在使用jQuery / jQuery Mobile的运行时检查/选择单选按钮,复选框和列表样式时遇到问题。

Basically I have a list of checkboxes, radio buttons and select menus and what I would like to do is get the value from the database and select/tick the correct item from the checkbox/radio button group or select menu. 基本上,我有一个复选框,单选按钮和选择菜单的列表,我想做的是从数据库中获取值,然后从复选框/单选按钮组或选择菜单中选择/勾选正确的项目。

Here is a simple setup of what I have tried, but it did not work. 这是我尝试过的简单设置,但是没有用。

HTML: HTML:

<fieldset data-role="controlgroup">     
  <input type="radio" name="option_radio" id="option_radio_1" value="1" />
  <label for="option_radio_1">Option 1</label>  
  <input type="radio" name="option_radio" id="option_radio_2" value="2" />
  <label for="option_radio_2">Option 2</label>
</fieldset>

<fieldset data-role="controlgroup">     
  <input type="checkbox" name="option_checkbox" id="option_checkbox_1" value="1" />
  <label for="option_checkbox_1">Option 1</label>   
  <input type="checkbox" name="option_checkbox2" id="option_checkbox_2" value="2" />
  <label for="option_checkbox_2">Option 2</label>   
  <input type="checkbox" name="option_checkbox3" id="option_checkbox_3" value="3" />
  <label for="option_checkbox_3">Option 3</label>
</fieldset>

<select name="option_list" id="option_list">
  <option value="20">20</option>
  <option value="30">30</option>
  <option value="40">40</option>
  <option value="50">50</option>
  <option value="60">60</option>
  <option value="70">70</option>
</select> 

JS: JS:

$("#option_checkbox_1").val(option_id); 
$("#option_list").val(optionlist_id);
$('input:radio[name=gender]:checked').val(gender_id);

What I tried is passing the value from the database (example 1/2/3/4 etc) from the database directly to the element. 我尝试的是将数据库中的值(例如1/2/3/4等)从数据库直接传递到元素。

Can anyone tell me how this should be done exactly? 谁能告诉我该怎么做?

To check it or give a value: 要检查它或给它一个值:

$('#option_checkbox_1').attr('checked', 'checked');
$('input:radio[id=option_radio_1]').attr('checked', 'checked');
$("#option_list").val(50);

and for un-checking (by removing the attribute entirely) do 并取消选中(通过完全删除属性)来执行

$('#option_checkbox_1').removeAttr('checked');
$('input:radio[name=gender]:checked').removeAttr('checked');

I think here is what you want 我想这就是你想要的

$('#option_checkbox_'+option_id).attr('checked', 'checked');
$('input:radio[id='+gender_id+']').attr('checked', 'checked');
$("#option_list").val(optionlist_id);

$("#option_checkbox_1").val(option_id);

This is used to assign the value to the element . 这用于将值分配给element。

You can Element with attribute value selector to find the element . 您可以Element with attribute value selector查找元素。

var valueFromDB = "1";
$("input[type='radio']['"+valueFromDB+"']").attr("checked","true");
$("input[type="checkbox"]['"+valueFromDB+"']").attr("checked","true");
$("#option_list option[value="20"]").attr("selected","true");

Another Point . 另外一点。

id is an unique for an element . id是元素的唯一。 You can't use id for multiple elements . 您不能将id用作多个元素。

To select the input element that has a particular value: 要选择具有特定值的input元素:

$('input[value=' + valueFromDatabase + ']');

To then check/select that element: 然后检查/选择该元素:

$('input[value=' + valueFromDatabase + ']').prop('checked', true);

This can, of course, be combined with other selectors (such as the :radio , or :checkbox ): 当然,这可以与其他选择器(例如:radio:checkbox )组合:

$('input:radio[value=' + valueFromDatabase + ']').prop('checked', true);

If the value from the database contains any white-space, though, the value must be quoted in the selector, giving: 如果从数据库中的值中包含任何空格,但是,值必须在选择报价,并提供:

$('input[value="' + valueFromDatabase + '"]');

(Note the " characters surrounding the `valueFromDatabase.) (请注意, "围绕'valueFromDatabase字符)。

That JS will set the value of the elements, it won't check them. JS将设置元素的值,而不检查它们。 What you want to do is use an if statement to see if the value of the radio/checkbox/select matches that of the string you're pulling from the database. 您要做的是使用if语句查看radio / checkbox / select的值是否与您从数据库中提取的字符串的值匹配。 If it does you then want to set the attribute to be checked/selected like so 如果是这样,则您希望像这样设置要检查/选择的属性

// checkboxes/radios
$(this).attr('checked','checked')

// options
$(this).attr('selected','selected')

where $(this) = the input or the option 其中$(this)=输入或选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM