日期:2014-05-16  浏览次数:20335 次

js 参数传递
function A(a, b, c, d) {
 var html = '<form method="POST" name="simple">'
    + '      <input type="button" onClick="B(a,b,c,d)">'
    + '  </td></tr>'
+ '  </table> '      
+ '</form>';
  return html;
}

请问如何将A()中的参数传递到B()中?求指教,谢谢。
js?传值

------解决方案--------------------

'      <input type="button" onClick="B(\''+ a +'\',\''+ b +'\',\''+ c +'\',\''+ c +'\')">'
------解决方案--------------------
引用:
Quote: 引用:

这种形式只能传递字符串,如果参数是对象的话,可以在调用A函数时传递json字符串,就行了

比如A("{\"a\":1}",1,1,1);

则B函数接收到的参数a就是一个对象,
不过不建议这么做,麻烦且不太合理,还是换另外的方式合适


function A(a, b, c, d) {
 var html = '<form method="POST" name="simple">'
    + '      <input type="button" onClick="B(\'' + {\"a\":1} + '\',\'' + {\"b\":1} + '\',\'' + {\"c\":1} + '\',\'' + {\"d\":1} + '\')">' //这样写直接报错,请问格式应该是什么,谢谢。
    + '  </td></tr>'
+ '  </table> '      
+ '</form>';
   return html;
}

你都不按照字符串格式乱来,当然是错的
 + '<input type="button" onClick="B({\"a\":1},{\"b\":1},{\"c\":1},{\"d\":1})"....
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

这种形式只能传递字符串,如果参数是对象的话,可以在调用A函数时传递json字符串,就行了

比如A("{\"a\":1}",1,1,1);

则B函数接收到的参数a就是一个对象,