如何在输入类型文本框值中打印javascript变量,如php echo

时间:2016-11-07 07:17:45

标签: javascript jquery html textbox

有可能像javascript中的php如下....

checkboxes

我在body标签中输入 <?php $var1 = "Hello"; $var2 = "World"; ?> <input type="text" value="<?php echo $var1; ?>" /> <input type="text" value="<?php echo $var2; ?>" /> <script type="text/javascript"> var str = "abc,def,ghi,jkl,mno,pqr"; var x = str.split(","); </script> ,如

type='text'

感谢。

4 个答案:

答案 0 :(得分:0)

&#13;
&#13;
var str = "abc,def,ghi,jkl,mno,pqr";
var x = str.split(",");

var input = document.getElementById("txtOne");
input.value = input.value + x[0];
&#13;
input {
    width: 400px;
  }
&#13;
<input id="txtOne" name="txtOne" type="text" value="I wan't print here using javascript " />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

不使用任何库,您需要使用DOM。

这可能是你想要的。

请参阅下面的DEMO

&#13;
&#13;
            $K=0;
        while($ROW = $RESULT->fetch_assoc() )
                {

                    if($ROW['type'] == $_GET['separate_id'])
                {
                    $DATE[] = $ROW['end_time'];
                    $meta_values[] = $ROW['meta_values'];

                    $body             = '


<table style="border:1px solid #000; border-collapse:collapse;">
<thead style="border:1px solid #000; padding:8px;width:150px; height:25px;font-size:15px">
';
foreach($DATE as $DATES)
{
        $DATESVIEW = explode('T',$DATES);
        $body .=' <th colspan="2">'.$DATESVIEW[0].'</th> ';
}

$body             .= '
</thead>



';
                foreach($meta_values as $VALS)
                {

                    $META_VALS = explode(' | ',$VALS);
                    foreach($META_VALS as $VALUEs)
                    {
                        $REs = explode(' - ', $VALUEs);
                    $body             .= '

  <tbody>
<tr style="border:1px solid #000; padding:8px;width:150px; height:25px;font-       size:15px">
            <td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[0].'</td>
        <td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[1].'</td>

</tr>
 </tbody>
';
                    }
                    }



                }
                   $K++;
                    }
                $body             .= '


  </table>

';
&#13;
public class VRPlayerCtrl : NetworkTransform {

//source gameobjects head, left and right controller object of htc vive prefab

public GameObject rightContSource;

public GameObject leftContSource;

public GameObject headObjSource;


//prefabs to assign head, left controller, and right controller
public GameObject vrHeadObjPrefab;
public GameObject vrLeftCtrlPrefab;
public GameObject vrRightCtrlPrefab;

GameObject vrHeadObj;
GameObject vrLeftCtrl;
GameObject vrRightCtrl;

void Start()
{

    Debug.Log("Start of the vr player");

    if (isLocalPlayer)
    {
        //instantiate prefabs
        CmdInstantiteHeadAndController();
        //disabled conroller meshes at VR player side so it cannont be view by him
        vrLeftCtrl.GetComponent<MeshRenderer>().enabled = false;
        vrRightCtrl.GetComponent<MeshRenderer>().enabled = false;
    }
}

//Instantiate on start head and vr controller object so that it can be view by normal players
void CmdInstantiteHeadAndController()
{
    Debug.Log("instantiateing the controller and head object");
    vrHeadObj = (GameObject)Instantiate(vrHeadObjPrefab);
    vrLeftCtrl = (GameObject)Instantiate(vrLeftCtrlPrefab);
    vrRightCtrl = (GameObject)Instantiate(vrRightCtrlPrefab);

    // spawn the bullet on the clients
    NetworkServer.Spawn(vrHeadObj);
    NetworkServer.Spawn(vrLeftCtrl);
    NetworkServer.Spawn(vrRightCtrl);
}

void Update()
{
    if (!isLocalPlayer)
    {
        return;
    }

    //sync pos on network
    CmdControllerPositionSync();
}
//sync position on VR controller objects so that VR player movemnts/action can be viewd by normal user
[Command]
public void CmdControllerPositionSync()
{

    vrHeadObj.transform.localRotation = headObjSource.transform.localRotation;
    vrHeadObj.transform.position = headObjSource.transform.position;
    vrLeftCtrl.transform.localRotation = leftContSource.transform.localRotation;
    vrRightCtrl.transform.localRotation = rightContSource.transform.localRotation;
    vrLeftCtrl.transform.localPosition = leftContSource.transform.position;
    vrRightCtrl.transform.localPosition = rightContSource.transform.position;
}

}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

Create Cookies in page 1 (Create Cookies On this page )
    <script type="text/javascript">
         function createCookie(name,value,days) {
              if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
          }
          else var expires = "";
               document.cookie = name+"="+value+expires+"; path=/";
          }
   var PartyDetail = $("#firstname").val() +"|"+$("#middlename").val()+"|"+$("#lastname").val();

  createCookie('cookiesPartyDetail',PartyDetail,2);
    </script>

  Now read cookies on second page

 <script type="text/javascript">
      function readCookie(name) {
          var nameEQ = name + "=";
          var ca = document.cookie.split(';');
          for(var i=0;i < ca.length;i++) {
               var c = ca[i];
               while (c.charAt(0)==' ') c = c.substring(1,c.length);
               if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
             }
            return null;
       }
     window.onload = function()
      {
          var str = readCookie('cookiesPartyDetail');
          //var str = "firstname,middlename,lastname";
          var x = str.split("|");
          for (i = 0; i < 3; i++) 
          { 
            $(".pending_order_"+[i]).val(x[i]);
          }

      } 

  function delete_cookie(name) {
    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
     }
 </script>

 <input class="pending_order_0" id="firstname" name="firstname" type="text"  /> 
 <input class="pending_order_1" id="middlename" name="middlename" type="text" />
 <input class="pending_order_2" id="lastname" name="lastname" type="text" />

 if I want to use again in input type hidden field same like firstname, middlename, lastname my code is here..

 <input class="pending_order_0" id="firstname" name="firstname" type="hidden"  /> 
 <input class="pending_order_1" id="middlename" name="middlename" type="hidden" />
 <input class="pending_order_2" id="lastname" name="lastname" type="hidden" />

答案 3 :(得分:-1)

使用 Javascript或其中一个库,您可以这样做。

使用Jquery:

$("#txtOne").val(x[0]);