2012-07-13 183 views
1

有一个名为fiche_tache_ref的text类型的HTML元素。内javascript文件(.js文件)页面的文件夹之外我想将其值设置根据列表框的选择值:如何使用JQuery设置文本HTML输入字段的值?

function changerNatureRefTache(nature_tache_id) { // nature_tache_id is from a listbox 

    var http_ajax = $('#http_ajax').val(); 
    var html = $.ajax({ 
    data: "nature_tache_id="+nature_tache_id , 
    type: "POST", 
    url: http_ajax+"referentiel/AjaxTacheChangerNatureTache.php" , 
    async: false 
    }).responseText; 

     // here I want to set the value of the field 

} 

AjaxTacheChangerNatureTache.php的准则是:

<?php 
    if (isset($_POST['nature_tache_id'])) { 
     define("ROOT_PATH", "../../"); 
     require_once ROOT_PATH . 'config.inc.php'; 
     require_once RP_MODELS . 'nature_tache.class.php'; 
     $db  = new DbConn(); 
     $obj = new nature_tache($db->getInstance()); 
     $ret = $obj->recupererNatureTache($_POST['nature_tache_id']); 
     $code = $ret['nat_code']; 
     echo $code; 
    } 
?> 

所以如何使用JQuery设置字段的值?

回答

4

使用val()功能

$('#idofinput').val("text you want in the field"); 

由于您使用的名称,你可以做以下

$('input[name=fiche_tache_ref]').val("text you want in the field"); 

由于名称不一定是唯一要小心使用该名称作为一个选择。我通常会建议使用一个id来代替。

+1

你打了我一分钟.. :-) – Rasmus 2012-07-13 12:08:19

+0

啊好吧+1无论如何为正确的答案哈哈。 – 2012-07-13 12:09:24

2

我不正确地得到你,但这样的事情是用来设置输入元素的值jQuery中: -

$("#elementid").val("myvalue"); 
0

你会得到很多相同的回答这个问题,因为这是一种在jQuery的文档低悬的果实的。

$("selector").val("new value"); 

但一个提示,我给你,这将使你的PHP干净了一点是方法而不是像这样的根路径:

define("ROOT_PATH", "../../"); 

你可以设置一个包括一个.htaccess路径使用下面的代码文件:

php_value include_path ".:/path/to/your/application" 

,然后你可以要求相对于路径(即require_once('config.inc.php'),而不是你的本质在做,这是require_once('../../config.inc.php')文件。

.htaccess文件应该位于保存应用程序的主目录中。

相关问题