[英]how to use JavaScript for reseting text field value?
I have following code: 我有以下代码:
<div class="form-group">
<?= $form->labelEx($model, 'membership', array('class' => 'col-xs-12 col-sm-4 control-label')) ?>
<div class="col-xs-12 col-sm-3">
<?= $form->dropDownList($model, 'membership', array(Yii::t('member', 'Yes'), Yii::t('member', 'MO')), array(
'class' => 'form-control',)); ?>
</div>
<?= $form->error($model, 'membership') ?>
</div>
and 和
<div class="col-xs-12 col-sm-3">
<?= $form->textField($model, 'question', array('class' => 'form-control dotted')) ?>
<?= $form->error($model, 'question') ?>
</div>
I need to develop a JavaScript file which resets textField, when user chooses 'NO' option from the drop down menu. 当用户从下拉菜单中选择“否”选项时,我需要开发一个可重置textField的JavaScript文件。 How can I do it?
我该怎么做?
Try this: 尝试这个:
// for Id selector
document.getElementById('element_id').value = '';
// for class selector
document.getElementsByClassName("element_class").value = '';
you can create javascript function like 您可以创建像
function myFunction(val) {
if(val=='NO')
{
document.getElementById('element_id').value = '';
}
}
and call it like this: <select onchange="myFunction(this.value)">
并这样调用:
<select onchange="myFunction(this.value)">
Try JS/JQuery
. 试试
JS/JQuery
。 apply onchange
function in dropdown
. 在
dropdown
应用onchange
函数。 In function check selected value and if it is NO then empty text field by using id 在功能中检查选定的值,如果为否,则使用id清空文本字段
below text_field_id
is id of your text field 以下
text_field_id
是您的文本字段的ID
<?= $form->dropDownList($model, 'membership', array(Yii::t('member', 'Yes'), Yii::t('member', 'MO')), array(
'class' => 'form-control','onchange'=>'validate(this.value)')); ?>
<script type="text/javascript">
function validate(value)
{
if(value == 'NO') {
document.getElementById('text_field_id').value = '';
}
}
</script>
First of all add onchange function in <select>
as: 首先在
<select>
添加onchange函数为:
<?=$form->dropDownList($model, 'membership', array(Yii::t('member', 'Yes'), Yii::t('member', 'MO')),
array('class' => 'form-control','onchange'=>'getAlert(this.value)')); ?>
Than in javascript: 比在javascript中:
<script type="text/javascript">
function getAlert(value){
if(value == 'No'){
document.getElementsByName("membership").value = '';
}
}
</script>
$( "selector_class_or_id" ).change(function() {
var selected_value = $(this).val();
if(selected_value === 'NO'){
$("textField_class_or_id").val('');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.