2012-07-13 12:05



There is a HTML element of type text whose name is fiche_tache_ref. Inside a javascript file (.js) outside of the page's folder I want to set its value according to the value of a listbox selected :

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

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


Code of AjaxTacheChangerNatureTache.php is :

    if (isset($_POST['nature_tache_id'])) {
        define("ROOT_PATH", "../../");
        require_once ROOT_PATH  . '';
        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;

So how to set the value of the field using JQuery ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dqo58772 dqo58772 9年前

    Use the val() function

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

    Since you are using name you can do the following

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

    Since names are not necessarily unique be careful with using the name as a selector. I would generally advise using an id instead.

    点赞 评论 复制链接分享
  • dongxie2749 dongxie2749 9年前

    you'll get lots of the same answer to this question, because this is kind of the low hanging fruit in the jquery docs.

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

    but one tip I'll give you that will make your php a little cleaner is instead of defining a root path like this:

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

    you can set an include path in an .htaccess file using the following code:

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

    and then you can require files relative to that path (i.e. require_once('') as opposed to what your essentially doing, which is require_once('../../').

    The .htaccess file should live in the main directory that holds your application.

    点赞 评论 复制链接分享
  • douwei1930 douwei1930 9年前

    Use the val() function of Jquery.

    $('#IdOfTextbox').val("Some Text");

    Further Reference

    点赞 评论 复制链接分享
  • doushan2811 doushan2811 9年前

    I dont get you correctly but something like this is used to set the value of input element in jquery:-

    点赞 评论 复制链接分享