我试图在JSON响应中发送一个函数,并试图在js中提醒该函数.下面是代码.
JS:
$('input').keyup(function(){
$.ajax({
type: "POST",
url: "sample.php",
dataType : 'json',
data: 'val='+$('input').val(),
success: function(response){
var json = transport.responseText.evalJSON();
alert(json.function()); // => **should alert 'foo bar' - but not**
}
});
});
Run Code Online (Sandbox Code Playgroud)
PHP:
<?php
// Our sample array
$foo = array(
'string' => 'bar',
'function'=> 'function(){return "foo bar";}'
);
$value_arr = array();
$replace_keys = array();
foreach($foo as $key => &$value){
// Look for values starting with 'function('
if(strpos($value, 'function(')===0){
// Store function string.
$value_arr[] = $value;
// Replace function string …Run Code Online (Sandbox Code Playgroud) 当附加到div时,我得到输入框值为undefined.下面是代码.
JS:
$('#submitHook').click(function () {
var v = ('#try').val();
alert(v);
$('#container').prepend('<tr><td>' + v + '</td></tr>');
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<code>
<input type="text" id="try"></input>
<input type="button" id="submitHook"></input>
<table id="container></table>
</code>
Run Code Online (Sandbox Code Playgroud)
同样如何将其扩展到textarea?感谢任何帮助.