js获取服务器控件button,怎么给button 赋值

JavaScript015

js获取服务器控件button,怎么给button 赋值,第1张

是文本框 <input type="button">这种么 如果是的话 直接$("input[type=button]").val("你想写啥就写啥") 就能完成赋值了

这个只需要用ajax来完成就行了,我这里用jquery的ajax方法,传递给PHP做个范本  能否看懂 就看你造化了,提醒一下  因为包含了php的代码  你需要在运行环境下才能看到效果

首先建立一个index.html

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

</head>

<body>

<div class="divCon">一月七号</div>

<input type="button" value="提交" class="button">

<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript">

$(".button").click(function(){

var con = $(".divCon").text()

$.ajax({

type:'POST',

url:"./submit.php",

data:{"cons":con},

dataType:"json",

success: function(data){

alert(data)//弹出返回值

},

error: function(data){

//alert(data.responseText)

}

})

})

</script>

</body>

</html>

再建立一个submit.php

<?php

$con = $_POST['cons']//获取前台传递的数据

if(!empty($con)){//判断是否不合法数据,empty 具体用法  请百度

exit(json_encode("您传输的数据是:{$con}"))//这里是给前台的返回值

}

//现在数据得到了  就可以根据PHP操作数据库的代码来进行操作就行了

<input type="text" id="t" />

<input type="button" id="b" />

document.getElementById('t').value=document.getElementById('b').id