2016-02-29 100 views
0

我需要修改使用CakePHP和JQuery在輸入表單中創建的標籤。將標籤修改爲輸入

$info = "test"; 
echo $this->Form->input('tipo_de_venta', array('type' => 'select', 'label' => 'Tipo de venta'.$info, 'div' => 'required', 'options' => $opciones_tipo_venta)); 

我在做什麼是設置一個變量$info用默認值,並更改每次選擇更改值的選項。根據選擇

$('#tipo_de_venta').on('change', function() { 
    console.log(this); 
    if(this.value == 1){ 

    } 
    if(this.value == 0){ 

    } 
    if(this.value == 2){ 

    } 
}); 

嘗試$(this).text("Change");但它不適用於我。

HTML:

<div class="required"> 
    <label for="EventoTipoDeVenta">Tipo de venta</label> 
    <select name="data[Evento][tipo_de_venta]" id="EventoTipoDeVenta"> 
     <option value="0">Compra simple</option> 
     <option value="1">Change</option> 
     <option value="2" selected="selected">Compra multiple simplificada</option> 
    </select> 
</div> 
+0

您可以添加最初呈現的HTML? – RRK

+0

最初的HTML?你什麼意思? – hateful

+0

從頁面源加載頁面時的輸入和其他元素的html。 – RRK

回答

1

試試這個代碼

$('#tipo_de_venta').on('change', function() { 
    console.log(this); 
    if(this.value == 1){ 
     $(this).closest('div').find('label').text('One'); 
    } 
    if(this.value == 0){ 
     $(this).closest('div').find('label').text('zero'); 
    } 
    if(this.value == 2){ 
     $(this).closest('div').find('label').text('two'); 
    } 
}) 
0

這裏是我的情況下工作的代碼,

$(this).closest('div').find('label').html('Change')