我需要在js对象中保留每个输入的值。 这是我的HTML代码:
<div class="produs_varianta">
<input type="text" class="product_name" value="Product One">
<input type="text" class="product_code" value="Code One">
</div>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product Two">
<input type="text" class="product_code" value="Code Two">
</div>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product Three">
<input type="text" class="product_code" value="Code Three">
</div>
我的.js代码:
var variante = $('.produs_varianta');
var produs_varianta = [];
variante.each(function(index){
produs_varianta.push(
{
'produs' : 'the value of product_name',
'cod' : 'the value of product_code
}
);
});
** 我尝试使用$(this).children(&#39; .product_name&#39;)。val()但我没有定义! **
答案 0 :(得分:3)
在.find()
$(this).find(".class").val()
var variante = $('.produs_varianta');
var produs_varianta = [];
variante.each( function() {
produs_varianta.push({
'produs': $(this).find(".product_name").val(),
'cod': $(this).find(".product_code").val()
});
});
console.log(produs_varianta)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product One">
<input type="text" class="product_code" value="Code One">
</div>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product Two">
<input type="text" class="product_code" value="Code Two">
</div>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product Three">
<input type="text" class="product_code" value="Code Three">
</div>
答案 1 :(得分:1)
您需要为此添加jquery函数。你的HTML是
<div class="produs_varianta">
<input type="text" class="product_name" value="Product One">
<input type="text" class="product_code" value="Code One">
</div>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product Two">
<input type="text" class="product_code" value="Code Two">
</div>
<div class="produs_varianta">
<input type="text" class="product_name" value="Product Three">
<input type="text" class="product_code" value="Code Three">
</div>
JQuery就是这个
var result = [];
$('.produs_varianta').each(function( ) {
var elem = $(this);
var obj = {
'product_name': elem.find('.product_name').val(),
'product_code' : elem.find('.product_code').val()
};
result.push(obj);
});
console.log(result);
以下是JSFIDDLE
的附加链接答案 2 :(得分:1)
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index', 'partial', 'complete', 'answer', 'answer-test'],
'allow' => true,
'roles' => ['@'],
],
],
'denyCallback' => function($rule, $action) {
return Yii::$app->getResponse()->redirect('denied')->send();
},
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'update' => ['post'],
'test-partial' => ['post'],
'test-complete' => ['post'],
'test-answer' => ['post'],
],
],
[
'class' => ContentNegotiator::className(),
'only' => ['update', 'answer'],
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
];
}