gpt4 book ai didi

javascript - 无法使用 jquery 选择或更改选择到 jq-idealform radiofield

转载 作者:行者123 更新时间:2023-11-28 08:52:29 25 4
gpt4 key购买 nike

我正在使用jq-idealforms构建一个漂亮且简单的表单。在其中我有一个 radio 字段,我想在从数据库获取信息后用 jquery 填充它。 Here有一个例子(在“step2”..第2节)您可以观看jq-ideal和radio-field如何工作。它不提供 API 来执行此操作,但如果我可以使用 jquery 填充 input[type='text'] 元素,那么它也必须可以使用单选字段。

你可以看看here ,jq-idealforms 如何管理 radio 场。

我尝试过以下部分代码:

  $(this).prop('checked', true);
$(this).next().addClass('checked');

其中 $(this) 是 3 个单选字段之一,也是我想要选择的字段(其余代码 is on the example ):

  <input type="radio" name="radio" style="position: absolute; left: -9999px;" autocomplete="off">

“checked”类未放入元素 span.ideal-radio (输入的同级元素)中。我对 DOM 很确定,因为如果我使用另一个类,比如“foo”,这个类就会被添加。我认为这是插件的错误,但是什么呢?我该如何解决?

然后我重新加载表单,如文档中所述: $(form).data('idealforms').reload().fresh();

文本区域元素和输入文本类型已正确填充。

编辑:

问题更简单,如果启用了 jq-idealforms,我无法选择单选框的第一个元素,如果我不使用 jq-idealforms,则选择第一个单选元素。或者,如果您将“checked”放在第二个单选元素上并使用 jq-idealforms,它也可以工作。

<html>
<head>
<script src="js/lib/jquery-1.8.2.min.js"></script>
<link rel="stylesheet" href="css/jquery.idealforms.min.css" media="screen"/>
<script src="js/lib/jquery.idealforms.min.js"></script> <!-- removing it work -->
<head>
<body>
<form id="form-doc" method="post" action="">
<div>
<label>Tipo doc:</label>
<input type="text" name="tipo_doc" />
</div>
<div>
<label>Ruolo ditta:</label>
<label>
<input type="radio" name="ruolo_ditta" checked/>Fornitore
</label>
<label>
<input type="radio" name="ruolo_ditta" /> Cliente
</label>
</div>
<div>
<button type="submit">Salva</button>
<button id="reset" type="button">Reset</button>
</div>
</form>
<script type="text/javascript">
var options = {
onFail: function() {alert( 'fail');},
onSuccess: function() {alert( 'succes');},
inputs: {
'tipo_doc' : {
filters: 'required min max',
data: { min: 3, max: 16 },
},
'ruolo_ditta' : {
filters: 'required min',
data: { min: 1 },
},
}
};
var myform = $('#form-doc').idealforms(options).data('idealforms');
$('#reset').click(function(){
myform.reset().fresh().focusFirst();
});
myform.focusFirst();
</script>`
</body>
</html>

它不适用于 jquery-1.9.1 和 jquery-1.10.1。

解决方案:

单选按钮的文档是错误的。您可以阅读site :

required

The field is required. This filter ONLY works with text inputs (text, password, textarea). For select use exclude to exclude the default option. For radio and checkbox use min: 1 which will require at least one option to be checked.

但使用:

'ruolo_ditta' : {
filters: 'min',
data: { min: 1 },
}

或在 Idealforms 选项上组合“必需”过滤器(或仅必需过滤器),其中“ruolo_ditta”是单选元素的属性名称值。该表格做出了错误且非常奇怪的行为。

删除它,在第一个单选 html 元素上使用选中效果很好,

$(this).prop('checked', true).trigger('change'); 

按方面工作。

谢谢,j.

最佳答案

如果您要更改值,则必须在表单上调用reload project doc

关于javascript - 无法使用 jquery 选择或更改选择到 jq-idealform radiofield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com