我的页面上有隐藏的输入,如下所示:
addToRequiredFields()
当人们填写相关表格时,需要某些其他字段(即从国家/地区下拉列表中选择“USA”时需要使用State。)
我有两个函数,一个名为removeFromRequiredFields()
,另一个名为function addToRequiredFields(string) {
var required = $('input[name=required]').val();
required += ',' + string;
$('input[name=required]').val(required);
}
function removeFromRequiredFields(string) {
var required = $('input[name=required]').val();
required.replace(',' + string, '');
$('input[name=required]').val(required);
}
,因为选择/取消选择了美国,但是删除一个似乎不起作用,我可以'弄清楚原因。
.on('change')
该功能在Select下拉列表的<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewReport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/images"
tools:context="litifer.awesome.game.cardview.MainActivity">
//-----------
//-----------
</LinearLayout>
</ScrollView>
处调用。
答案 0 :(得分:0)
虽然它不是根据输入值验证控件的正确方法,但我为你制作了一个脚本。
function addToRequiredFields(string) {
var required = $('input[name=required]').val();
fieldsArray = required.split(",")
fieldsArray.push(string)
$('input[name=required]').val(fieldsArray.join());
}
function removeFromRequiredFields(string) {
var required = $('input[name=required]').val();
fieldsArray = required.split(",")
fieldsArray = fieldsArray.filter(function(item) {
return item !== string
})
$('input[name=required]').val(fieldsArray.join());
}
console.log($('input[name=required]').val())
addToRequiredFields("age")
console.log($('input[name=required]').val())
removeFromRequiredFields("name")
console.log($('input[name=required]').val())
removeFromRequiredFields("tel")
console.log($('input[name=required]').val())
addToRequiredFields("name")
console.log($('input[name=required]').val())
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="hidden" name="required" value="name,town,tel,email">
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</body>
</html>
答案 1 :(得分:0)
在您的删除功能中使用以下登录.removeFromRequiredFields
//var requiredArr = $('input[name=required]').attr('value');
var requiredArr = "name,town,tel,email".split(',');
var rmvIndex = requiredArr.indexOf("tel")//"tel" is your string to be removed
var newArr = requiredArr.splice(rmvIndex, 1);
$('input').attr('value', newArr.join(','))// set value attribute after removing that string