admin管理员组文章数量:1322017
I formed a Json String.
var jsonProduct = "{Product:'" + Details[0] + "',Brand:'" + Details[1] + "',Model:'" + Details[2] + "',Price:'" + Details[3] + "'}"
<input class="button black" type="submit" value="Add To Cart" onclick="return addOrderItem(' + jsonProduct + ')" />
How to pass this 'jsonproduct to javascript function addOrderItem as follows
function addOrderItem(product)
{
cartproduct[cartproduct.length] = " + product + ";
//cartproduct[cartproduct.length] = " + {Product:'1001',Brand:Dell',Model:'Inspiron',Price:'25000'} + ";
}
When I pass product as parameter it is not working
I formed a Json String.
var jsonProduct = "{Product:'" + Details[0] + "',Brand:'" + Details[1] + "',Model:'" + Details[2] + "',Price:'" + Details[3] + "'}"
<input class="button black" type="submit" value="Add To Cart" onclick="return addOrderItem(' + jsonProduct + ')" />
How to pass this 'jsonproduct to javascript function addOrderItem as follows
function addOrderItem(product)
{
cartproduct[cartproduct.length] = " + product + ";
//cartproduct[cartproduct.length] = " + {Product:'1001',Brand:Dell',Model:'Inspiron',Price:'25000'} + ";
}
When I pass product as parameter it is not working
Share Improve this question edited May 31, 2013 at 11:10 Isaac 11.8k5 gold badges35 silver badges45 bronze badges asked May 31, 2013 at 11:01 user2440125user2440125 311 gold badge1 silver badge3 bronze badges 1- Why are you handling the click on a submit input ? Don't you want to handle the submit event on the form instead ? – Denys Séguret Commented May 31, 2013 at 11:07
1 Answer
Reset to default 4You could parse it using
var product = JSON.parse(jsonProduct);
but you don't have to use JSON at all. Do this :
var product = {
Product: Details[0], Brand:Details[1],
Model:Details[2], Price:Details[3]
};
addOrderItem(product);
If you want to call this from an input click, you can bind the call using
onclick="return addOrderItem(product)"
or, better, give an id to your element and then bind the event handler from the JS code :
<input id=submit class="button black" type="submit" value="Add To Cart">
<script>
document.getElementById('submit').onclick=function(){addOrderItem(product)};
</script>
本文标签: How to pass Json string as a javascript method parameterStack Overflow
版权声明:本文标题:How to pass Json string as a javascript method parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742112856a2421331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论