2017-09-05 111 views
0

我可以在某些函數之後設置或向輸入字段發送值。我可以根據需要在執行某些操作後顯示我的值而不是佔位符。 它只適用於單個輸入字段,所以我怎樣才能做到兩個或更多的輸入字段(輸入類型是文本)? 工作代碼如下,如何在javascript中設置多個輸入字段的值from函數

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
     $("input:text").val("Glenn Quagmire"); 

    }); 
}); 
</script> 
</head> 
<body> 

<p>Name: <input type="text" name="user" placeholder="entername"></p> 


<button>Set the value of the input field</button> 

</body> 
</html> 

這樣我想點擊按鈕後,設置了兩個輸入字段的值。我怎樣才能使其功能?

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
     $("input:text").val("Glenn Quagmire"); 
     $("input.text").val("Kathmandu"); 
    }); 
}); 
</script> 
</head> 
<body> 

<p>Name: <input type="text" name="user" placeholder="entername"></p> 
<p>Add : <input type="text" name="add" placeholder="address"></p> 

<button>Set the value of the input field</button> 

</body> 
</html> 

在此先感謝您。

+0

應在'user'和'add'中添加什麼? –

+0

使用'$(「input.text」)。each(function(){// stuff})循環所有文本;'儘管如果不希望所有輸入都包含相同的值,則需要適當的處理。 – masterpreenz

+0

這會設置用戶:'$('input [name =「user」]')。val('my_user');' – Mehdi

回答

0

您可以使用其名稱屬性爲不同的輸入字段設置值。

$(document).ready(function(){ 
    $("button").click(function(){ 
     $('input[name="user"]').val("Glenn Quagmire"); 
     $('input[name="add"]').val("Kathmandu"); 
    }); 
}); 
相關問題